package tetris.game;

import tetris.game.menu.GenericMenu;
import tetris.game.model.Game;
import tetris.game.view.TetrisGLView;
import android.app.Activity;
import android.content.Intent;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;

public class MucisTetrisActivity extends Activity {
	
	private TetrisGLView view;
	private GLSurfaceView mGLView;
	private GenericMenu gM;
	private int width, height;
	
	private Game game;
	private GameLoopThread gameLoopThread;
	
	private Handler handler;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.width = this.getWindowManager().getDefaultDisplay().getWidth();
        this.height = this.getWindowManager().getDefaultDisplay().getHeight();
        
	    handler = new Handler();
	    
	    // set audio volume to maximum 
	    // Get the AudioManager
	    /*	AudioManager audioManager = 
	    	(AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
	    	// Set the volume of played media to maximum.
	    	audioManager.setStreamVolume (AudioManager.STREAM_MUSIC,
	    			audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);	*/
	    
        // Create a GLSurfaceView and a GameLoop
	    game = new Game();
	    view = new TetrisGLView(this, game);
        mGLView = view;
        gameLoopThread = new GameLoopThread(this, mGLView, game, handler);
        gameLoopThread.start();        
        
        setContentView(mGLView);
    }
    
    @Override
    protected void onPause() {
    	super.onPause();
    	mGLView.onPause();
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	mGLView.onResume();
    }
    
    @Override
	protected void onDestroy() {
		super.onDestroy();
		mGLView.onPause();
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	gM = new GenericMenu(menu);
    	gM.populate();
    	return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	if (item.getTitle().equals("Restart")) {
    		// create new game
    		game = new Game();
    		gameLoopThread.setGame(game);
    		view.game = game;
    		
    		// init
    		gameLoopThread.setShowGOMessage(true);
    	}
    	
    	if (item.getTitle().equals("Quit")) {
    		gameLoopThread.setRunning(false);
    		gameLoopThread.destroy();
    		this.setResult(0, new Intent());
    		this.finish();
    	}
    	
    	return super.onMenuItemSelected(featureId, item);
    }
}